function glovetime = decodetimestamp(timestamp)
%Decodes the 5 Bytes of the Glove Data that encodes the Time
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%     Logic is as follows:
%     Timebytes consist of 5 bytes (1 Encoding Byte + 4 Time Counter Bytes)
%     Last 4 Bits of Encoding Byte tells you if any of the following bytes are Null
%     If the One of the Following Bits is zero, then in addition to the Encoding Byte
%     being zero, that corresponding Byte is set to 1 (Double Check)
%     
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    encodebyte=timestamp(1);
    errflag=0;   
    
    %Convert Encoding Byte to 8 bits
    c=dec2bin(encodebyte,8);
    
    %Leading Bit of Encoding Byte always has to be 1
    if(c(1)~='1')
        errflag = 1;
    end

    %Check for last 4 bits - If any of them is ONE, corresponding time stamp byte is actually zero
    %Also check if corresponding byte is set to 1
    if(c(8) == '1')
        if(timestamp(2)==1)
            flag_byte1 = 0;
        else
            errflag=1;
        end
    else
        flag_byte1 = 255;
    end

    if(c(7) == '1')
        if(timestamp(3)==1)
            flag_byte2 = 0;
        else
            errflag=1;
        end
    else
        flag_byte2 = 255;
    end

    if(c(6) == '1')
        if(timestamp(4)==1)
            flag_byte3 = 0;
        else
            errflag=1;
        end
    else
        flag_byte3 = 255;
    end

    if(c(5) == '1')
        if(timestamp(5)==1)
            flag_byte4 = 0;
        else
            errflag=1;
        end
    else
        flag_byte4 = 255;
    end

    if(errflag == 1) %Data Corrupted or Error occured
        glovetime = NaN;
    else
        BYTE1 = bitand(timestamp(2),flag_byte1);
        BYTE2 = bitand(timestamp(3),flag_byte2);
        BYTE3 = bitand(timestamp(4),flag_byte3);
        BYTE4 = bitand(timestamp(5),flag_byte4);
        
        TIMER=[dec2bin(BYTE4,8) dec2bin(BYTE3,8) dec2bin(BYTE2,8) dec2bin(BYTE1,8)];
        glovetime=bin2dec(TIMER)*8.68048e-6;
    end